#ifndef SINGLETON_H
#define SINGLETON_H

#include "global.h"
#include <mutex>

template <typename T>
class Singleton
{
public:
    static std::shared_ptr<T> GetInstance()
    {
        static std::once_flag flag;
        std::call_once(flag, [&]() {
            m_pInstance = std::shared_ptr<T>(new T());
        });
        return m_pInstance;
    }

    ~Singleton() {
        std::cout << "This is singleton destruct" << std::endl;
    }

    void PrintAddress()
    {
        std::cout << "Singleton address: " << m_pInstance.get() << std::endl;
    }

protected:
    Singleton() = default;
    Singleton(const Singleton<T> &) = delete;       //禁止拷贝构造
    void operator=(const Singleton<T> &) = delete;  //禁止拷贝赋值

    static std::shared_ptr<T> m_pInstance;
};

// 模板类实例化
template <typename T>
std::shared_ptr<T> Singleton<T>::m_pInstance = nullptr;

#endif // !SINGLETON_H